package br.com.consignum.util;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class RestUtil {

    public static String post(final String url, final String parametros) {

	String retorno = null;

	try {
	    final HttpParams httpParams = new BasicHttpParams();
	    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);

	    final HttpClient httpclient = new DefaultHttpClient(httpParams);
	    final HttpPost httppost = new HttpPost(url);
	    httppost.setHeader("Content-Type", "application/json; charset=utf-8");
	    httppost.setHeader("Accept", "application/json");

	    httppost.setEntity(new StringEntity(parametros, "utf-8"));
	    final HttpResponse response = httpclient.execute(httppost);
	    retorno = EntityUtils.toString(response.getEntity(), "utf-8");

	} catch (final Exception e) {
	    e.printStackTrace();
	}

	return retorno;
    }
}
